package org.bbsoft.catchme.model.servlets;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bbsoft.catchme.model.TransactionService;
import org.bbsoft.catchme.model.EntityManagerService;
import org.bbsoft.catchme.model.entities.Event;
import org.bbsoft.catchme.model.entities.Location;
import org.bbsoft.catchme.model.entities.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet implementation class Test
 */
@WebServlet("/Test")
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@TransactionService
	@Inject
	EntityManagerService entityManagerService;

	Logger logger = LoggerFactory.getLogger(Test.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Test() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			entityManagerService.init();
			EntityManager entityManager = entityManagerService
					.getEntityManager();
		
			
			User user = entityManager.find(User.class, 1L);
			
			if(user!=null){
				
				user.getOwnedEvents().clear();
			}
		
			
			entityManager.merge(user);
			
			
		}

		finally {

			entityManagerService.release();

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
